package com.a1anwang.okble.server.advertise;

import com.a1anwang.okble.common.LogUtils;
import com.a1anwang.okble.common.OKBLEDataUtils;

import ohos.app.Context;
import ohos.bluetooth.BluetoothHost;
import ohos.bluetooth.ble.BleAdvertiseCallback;
import ohos.bluetooth.ble.BleAdvertiseData;
import ohos.bluetooth.ble.BleAdvertiseSettings;
import ohos.bluetooth.ble.BleAdvertiser;
import ohos.utils.SequenceUuid;

import java.util.Set;

/**
 * Created by a1anwang.com on 2018/5/30.
 */
public class OKBLEAdvertiseManager {
    private BleAdvertiser bleAdvertiser;
    private Context context;

    /**
     * 构造函数
     *
     * @param context 上下文
     */
    public OKBLEAdvertiseManager(Context context) {
        this.context = context;
        init();
    }

    /**
     * 初始化方法
     */
    public void init() {
    }

    private BleAdvertiseCallback bleAdvertiseCallback;


    /**
     * 开始广播
     *
     * @param okbleAdvertiseSettings 设置
     * @param data                   数据
     * @param okbleAdvertiseCallback 回调
     */
    public void startAdvertising(OKBLEAdvertiseSettings okbleAdvertiseSettings, OKBLEAdvertiseData data,
                                 final OKBLEAdvertiseCallback okbleAdvertiseCallback) {

        bleAdvertiseCallback = new BleAdvertiseCallback() {
            @Override
            public void startResultEvent(int result) {
                // 开始BLE广播成功
                if (okbleAdvertiseCallback != null) {
                    if (BleAdvertiseCallback.RESULT_SUCC == result) {
                        okbleAdvertiseCallback.onStartSuccess();
                    } else {
                        okbleAdvertiseCallback.onStartFailure(result, OKBLEAdvertiseFailedDescUtils.getDesc(result));
                    }
                }
            }
        };
        bleAdvertiser = new BleAdvertiser(context, bleAdvertiseCallback);
        BleAdvertiseSettings mSettings = new BleAdvertiseSettings.Builder()
                .setConnectable(okbleAdvertiseSettings.isConnectable())
                .setInterval(BleAdvertiseSettings.INTERVAL_SLOT_MIN)
                .setTxPower(BleAdvertiseSettings.TX_POWER_MAX)
                .build();
        BleAdvertiseData.Builder advertiseDataBuilder = new BleAdvertiseData.Builder();
        for (int key : data.getManufacturerSpecificData().keySet()) {
            byte[] value = data.getManufacturerSpecificData().get(key);
            advertiseDataBuilder.addManufacturerData(key, value);
            LogUtils.e("Manufacturer id:" + key + " data:" + OKBLEDataUtils.BytesToHexString(value));

        }
        int size2 = data.getServiceUuids().size();
        for (int i = 0; i < size2; i++) {
            SequenceUuid uuid = data.getServiceUuids().get(i);
            advertiseDataBuilder.addServiceUuid(uuid);
            LogUtils.e(" service uuid:" + uuid.getUuid().toString());
        }
        Set<SequenceUuid> keySet = data.getServiceData().keySet();
        for (SequenceUuid keyUuid : keySet) {
            LogUtils.e(" service data uuid:" + keyUuid.getUuid().toString() + " data:" + OKBLEDataUtils.BytesToHexString(data.getServiceData().get(keyUuid)));
            advertiseDataBuilder.addServiceData(keyUuid, data.getServiceData().get(keyUuid));
        }
        BleAdvertiseData mAdvertiseData = advertiseDataBuilder.build();
        bleAdvertiser.startAdvertising(mSettings, mAdvertiseData, null);
    }

    /**
     * 停止
     */
    public void stopAdvertising() {
        if (bleAdvertiser != null) {
            bleAdvertiser.close();
        }
    }
}
